from django.db import models

from game.ai.models import AVAILABLE_CONTROLLERS
from game.common.logger import logger

from polymorphic import PolymorphicModel

import random
from copy import deepcopy

# Flags for different encounters

class encounter(PolymorphicModel):
    #Relative probability of encounter occuring vs other encounters for an area
    message = models.TextField() #inital message to be displayed
    probability = models.IntegerField(default=1)
    
    def generateRewards(self):
        #returns an array of rewards for an encounter
        allRewards = reward.objects.filter(encounter=self)
        rewardsEarned = []
        for x in allRewards:
            if random.randint(1,x.probability) == x.probability:
                rewardsEarned.append((x.itemInstance,x.quantity))
        return rewardsEarned
    
class reward(models.Model):
    #The encounter that grants this reward
    encounter = models.ForeignKey('encounter.encounter')
    itemInstance = models.ForeignKey('inventory.itemInstance', null = True, default=None)
    quantity = models.IntegerField(default = 1)
    #Absolute probability of the reward being given out. Probability = to 1/X
    probability = models.IntegerField(default=1)
    
class encounterIndividual(models.Model):
#Is part of a combat encounter and spawns new individuals to take part in combat
    baseIndividual = models.ForeignKey('monster.individual')
    
    def spawnIndividual(self):
        newIndividual = deepcopy(self.baseIndividual)
        newIndividual.id = None
        newIndividual.save()
        return newIndividual
    
class encounterIndividuals(models.Model):
#Table for ManyToMany relations between a combatEncounter and an encounterIndividual
    individual = models.ForeignKey('encounter.encounterIndividual')
    encounter = models.ForeignKey('encounter.combatEncounter')
    quantity = models.PositiveIntegerField(default = 1)
    controller = models.IntegerField(default = 1, choices = AVAILABLE_CONTROLLERS)
    
    def getIndividuals(self):
        #returns an array of individuals for a single encounter group along with their AI controller
        individuals = []
        for x in xrange(self.quantity):
            logger.debug("Appending new individual number %s"%(x))
            individuals.append((self.individual.spawnIndividual(), self.controller))
        logger.debug("Generated all individuals: %s"%(individuals))
        return individuals
    
class combatEncounter(encounter):
    encounterIndividuals = models.ManyToManyField(encounterIndividual, through=encounterIndividuals)
    
    def getIndividuals(self):
        #returns an array of all the individuals and their controller for one encounter
        individuals = []
        for x in encounterIndividuals.objects.filter(encounter = self):
            for y in x.getIndividuals():
                individuals.append(y)
        logger.debug("All individuals for this encounter: %s"%(individuals))    
        return individuals
    

class engagedPlayer(models.Model):
    #represents a player involved in a combat encounter
    profile = models.ForeignKey('profile.profile', null = False, unique = True)
    encounter = models.ForeignKey('encounter.encounter', null = False, unique = True)
    combat = models.ForeignKey('combat.combat')
    